'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports UA
Imports System.IO
Imports TaskScheduler

Module Module1
    Sub main()
        If UnixDetected() = False Then
            'remove windows startup entries
            Dim RegKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
            If Format(RegKey.GetValue("UATrayIcon")) <> "" Then RegKey.DeleteValue("UATrayIcon")
            If Format(RegKey.GetValue("UAWallpaper")) <> "" Then RegKey.DeleteValue("UAWallpaper")
            RegKey.Close()

            'delete all existing shutdown entries
            Dim RootPath1 As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts"
            Dim RootPath2 As String = "SOFTWARE\Policies\Microsoft\Windows\System\Scripts"
            RemoveShutdownScript(Application.StartupPath & Path.DirectorySeparatorChar & "UAShutdownUpdate.exe", RootPath1)
            RemoveShutdownScript(Application.StartupPath & Path.DirectorySeparatorChar & "UAShutdownUpdate.exe", RootPath2)

            'go though all tasks and delete existing UAWallpaper tasks
            Dim NewScheduledTasks As ScheduledTasks
            Try
                NewScheduledTasks = New ScheduledTasks
                For Each TaskName As String In NewScheduledTasks.GetTaskNames
                    If TaskName.StartsWith("UAWallpaper") = True Then
                        NewScheduledTasks.DeleteTask(TaskName)
                    End If
                Next
            Catch ex As Exception
            Finally
                If NewScheduledTasks IsNot Nothing Then NewScheduledTasks.Dispose()
            End Try

            'delete gadget files
            If VistaDetected() = True Then
                Dim GadgetPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & Path.DirectorySeparatorChar & "Microsoft" & Path.DirectorySeparatorChar & "Windows Sidebar" & Path.DirectorySeparatorChar & "Gadgets" & Path.DirectorySeparatorChar
                Try
                    My.Computer.FileSystem.DeleteDirectory(GadgetPath & "UsageAgent.Gadget", FileIO.DeleteDirectoryOption.DeleteAllContents)
                Catch ex As Exception
                End Try
            End If

            'uninstall
            Shell("msiexec /x {F2979728-5C01-4D39-8974-DBC579C3BD49} /qr", AppWinStyle.Hide)
        End If
    End Sub
End Module
